﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.Objects;

namespace BusinessLogic.Command
{
    public class MaintainPersonalInfo : BaseCommand<APP_PERSONAL>
    {
        private APP_PERSONAL P;
        private string Hash = string.Empty;

        public MaintainPersonalInfo(APP_PERSONAL APP_PERSONALInfo)
        {
            P = APP_PERSONALInfo;
        }

        public MaintainPersonalInfo(string hash)
        {
            Hash = hash;
        }

        public override void Command()
        {
            var ctx = new CubeOfDB2Entities();

            if (Hash != string.Empty) //Delete
            {
                var P = ctx.APP_PERSONAL.Where(x => x.HASH == Hash).FirstOrDefault();

                var emper = ctx.APP_EMPLOYER.Where(x => x.PERSONAL_ID == P.ID).ToList();
                if (emper != null)
                {
                    foreach (var e in emper)
                    {
                        var pro = ctx.APP_PROJECT.Where(x => x.EID == e.ID).ToList();
                        foreach (var pj in pro)
                        {
                            var obj = ctx.APP_PROJECT.Where(x => x.ID == pj.ID).FirstOrDefault();
                            if (obj != null)
                            {
                                ctx.APP_PROJECT.DeleteObject(obj);
                                ctx.SaveChanges();
                            }
                        }

                        ctx.APP_EMPLOYER.DeleteObject(e);
                        ctx.SaveChanges();
                    }
                }

                var lang = ctx.APP_LANGUAGE.Where(x => x.PERSONAL_ID == P.ID).ToList();
                if (lang != null)
                {
                    foreach (var l in lang)
                    {
                        ctx.APP_LANGUAGE.DeleteObject(l);
                        ctx.SaveChanges();
                    }
                }

                var edu = ctx.APP_EDUCATION.Where(x => x.PERSONAL_ID == P.ID).ToList();
                if (edu != null)
                {
                    foreach (var e in edu)
                    {
                        ctx.APP_EDUCATION.DeleteObject(e);
                        ctx.SaveChanges();
                    }
                }

                var fam = ctx.APP_FAMILY.Where(x => x.PERSONAL_ID == P.ID).ToList();
                if (fam != null)
                {
                    foreach (var f in fam)
                    {
                        ctx.APP_FAMILY.DeleteObject(f);
                        ctx.SaveChanges();
                    }
                }

                ctx.APP_PERSONAL.DeleteObject(P);
                ctx.SaveChanges();
            }
            else
            {
                if (P.ID == 0 || P.ID == null) //Add
                {
                    ctx.APP_PERSONAL.AddObject(P);
                }
                else // Edit
                {
                    var objP = ctx.APP_PERSONAL.Where(x => x.ID == P.ID).FirstOrDefault();

                    if (objP != null)
                    {
                        objP.HEIGHT = P.HEIGHT;
                        objP.BLOOD_GROUP = P.BLOOD_GROUP;
                        objP.MILITARY = P.MILITARY;
                        objP.BIRTH_DATE = P.BIRTH_DATE;
                        objP.AGE = P.AGE;
                        objP.GENDER = P.GENDER;
                        objP.NATIONALITY = P.NATIONALITY;
                        objP.RELIGION = P.RELIGION;
                        objP.WEIGHT = P.WEIGHT;
                        objP.BIRTH_PLACE = P.BIRTH_PLACE;
                        objP.MARITAL = P.MARITAL;
                        objP.APP_DATE = P.APP_DATE;
                        objP.POSITION = P.POSITION;
                        objP.SRT_DATE = P.SRT_DATE;
                        objP.HOWTO_NOTE = P.HOWTO_NOTE;
                        objP.FNAME_TH = P.FNAME_TH;
                        objP.FNAME_EN = P.FNAME_EN;
                        objP.LNAME_TH = P.LNAME_TH;
                        objP.LNAME_EN = P.LNAME_EN;
                        objP.NNAME_TH = P.NNAME_TH;
                        objP.NNAME_EN = P.NNAME_EN;
                        objP.ADDRESS = P.ADDRESS;
                        objP.PROVINCE = P.PROVINCE;
                        objP.POSTCODE = P.POSTCODE;
                        objP.EMAIL = P.EMAIL;
                        objP.TEL = P.TEL;
                        objP.MOBILE = P.MOBILE;
                        objP.IMG = P.IMG;

                        /* Family and Education */
                        objP.IS_STUDYING = P.IS_STUDYING;
                        objP.STUDYING_INSTITUTE = P.STUDYING_INSTITUTE;
                        objP.STUDYING_MAJOR = P.STUDYING_MAJOR;
                        objP.STUDYING_CLASS = P.STUDYING_CLASS;
                        objP.STUDYING_FINISHON = P.STUDYING_FINISHON;


                        //Skill
                        objP.PRG_LANG_SKILL = P.PRG_LANG_SKILL;
                        objP.REPORT_SKILL = P.REPORT_SKILL;
                        objP.DB_SKILL = P.DB_SKILL;
                        objP.TOOLS_SKILL = P.TOOLS_SKILL;
                        objP.FRAMEWORK_SKILL = P.FRAMEWORK_SKILL;
                        objP.TESTING_SKILL = P.TESTING_SKILL;
                        objP.OS_SKILL = P.OS_SKILL;
                        objP.OTHER_SKILL = P.OTHER_SKILL;

                        //Expected Salary
                        objP.EXPECT_SALARY = P.EXPECT_SALARY;

                        //Status
                        objP.TX_STATUS = P.TX_STATUS;
                    }
                }
                ctx.SaveChanges();
            }
        }
    }
}
